#define MSTIME_C
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <signal.h>        //signal()
#include <sys/time.h>    //struct itimerval, setitimer()
#include <libmslog/mslog.h>
#include "mscommon.h"
#include "mstime.h"
#include "mstypes.h"

#define year_len		4
#define mon_len		2	
#define day_len		2
#define hour_len		2	
#define min_len		2	
#define sec_len		2	

//user header
#define FLAG "TIME"
static ms_void mstime_innerapi_parseDate(ms_string ms_in pstr, PRSTime ms_out prstime)	//2015-06-03 12:30:25
{
	#define parsedate_year_index	0
	#define parsedate_year_len		4
	#define parsedate_year_end		(parsedate_year_index+parsedate_year_len)
	#define parsedate_year_end_len	1	
	
	#define parsedate_mon_index		(parsedate_year_end+parsedate_year_end_len)
	#define parsedate_mon_len		2	
	#define parsedate_mon_end		(parsedate_mon_index+parsedate_mon_len)
	#define parsedate_mon_end_len	1
	

	#define parsedate_day_index		(parsedate_mon_end+parsedate_mon_end_len)
	#define parsedate_day_len		2
	#define parsedate_day_end		(parsedate_day_index+parsedate_day_len)
	#define parsedate_day_end_len	1	
	#define parsedate_rstime_val(pstr,targetbuf,iteam)	 \
		memset(targetbuf,0,sizeof(targetbuf));\
		memcpy(targetbuf,&pstr[parsedate_##iteam##_index],parsedate_##iteam##_len);\
		prstime->tm##iteam=atoi(targetbuf);
	
	ms_s08 buf[8];
	//ms_debug("DumpDate:%s", pstr);
	parsedate_rstime_val( pstr, buf,year);
	parsedate_rstime_val( pstr, buf,mon);
	parsedate_rstime_val( pstr, buf,day);
}

static ms_void mstime_innerapi_parseTime(ms_string ms_in pstr, PRSTime ms_out prstime)	//2015-06-03 12:30:25
{
	#define parsetime_hour_index	0
	#define parsetime_hour_len		2		
	#define parsetime_hour_end		(parsetime_hour_index+parsetime_hour_len)
	#define parsetime_hour_end_len	1	

	#define parsetime_min_index		(parsetime_hour_end+parsetime_hour_end_len)
	#define parsetime_min_len		2	
	#define parsetime_min_end		(parsetime_min_index+parsetime_min_len)
	#define parsetime_min_end_len	1

	#define parsetime_sec_index		(parsetime_min_end+parsetime_min_end_len)
	#define parsetime_sec_len		2		

	#define parsetime_rstime_val(pstr,targetbuf,iteam)	 \
		memset(targetbuf,0,sizeof(targetbuf));\
		memcpy(targetbuf,&pstr[parsetime_##iteam##_index],parsetime_##iteam##_len);\
		prstime->tm##iteam=atoi(targetbuf);
	/*
	sscanf(prstime, "%d-%d-%d %d:%d:%d", &rtc_time.tm_year,
        		&rtc_time.tm_mon, &rtc_time.tm_mday,&rtc_time.tm_hour,
        		&rtc_time.tm_min, &rtc_time.tm_sec);
	*/
	ms_s08 buf[8];
	//ms_debug("DumpDate:%s", pstr);
	parsetime_rstime_val( pstr, buf,hour);
	parsetime_rstime_val( pstr, buf,min);
	parsetime_rstime_val( pstr, buf,sec);
}
ms_void mstime_innerapi_parseDateTime(ms_string ms_in pstr, PRSTime ms_out ptm)	//20150603123025
{
	#define st_year_index	0
	#define st_year_len		4	
	
	#define st_mon_index	(st_year_index+st_year_len)
	#define st_mon_len		2		

	#define st_day_index		(st_mon_index+st_mon_len)
	#define st_day_len		2	

	#define st_hour_index	(st_day_index+st_day_len)
	#define st_hour_len		2			

	#define st_min_index		(st_hour_index+st_hour_len)
	#define st_min_len		2			

	#define st_sec_index		(st_min_index+st_min_len)
	#define st_sec_len		2

	ms_stru0(ptm, PRSTime);
	
	#define rstime_stt(pstr,targetbuf,iteam)	 \
		memset(targetbuf,0,sizeof(targetbuf));\
		memcpy(targetbuf,&pstr[st_##iteam##_index],st_##iteam##_len);\
		ptm->tm##iteam=atoi(targetbuf);
	
	unsigned char buf[8];
	//ms_debug("DumpDate:%s", pstr);
	rstime_stt( pstr, buf,year);
	rstime_stt( pstr, buf,mon);
	rstime_stt( pstr, buf,day);
	rstime_stt( pstr, buf,hour);
	rstime_stt( pstr, buf,min);
	rstime_stt( pstr, buf,sec);

	ms_verbose(mstime_fmt_datetime, ptm->tmyear,ptm->tmmon,ptm->tmday,ptm->tmhour,ptm->tmmin,ptm->tmsec);
	
}
ms_void mstime_api_parsetimestr1(ms_string ms_in pstr, PRSTime ms_out ptm_start, PRSTime ms_out ptm_stop,ms_ps08 ms_out outbuf)	//B20150603123025E20150603123025
{
	#define start_tag_index		0
	#define start_tag_len		1
	
	#define start_year_index	(start_tag_index+start_tag_len)
	#define start_year_len		4	
	
	#define start_mon_index	(start_year_index+start_year_len)
	#define start_mon_len		2		

	#define start_day_index		(start_mon_index+start_mon_len)
	#define start_day_len		2	

	#define start_hour_index	(start_day_index+start_day_len)
	#define start_hour_len		2			

	#define start_min_index		(start_hour_index+start_hour_len)
	#define start_min_len		2			

	#define start_sec_index		(start_min_index+start_min_len)
	#define start_sec_len		2

	#define stop_tag_index		(start_sec_index+start_sec_len)
	#define stop_tag_len		1
	
	#define stop_year_index	(stop_tag_index+stop_tag_len)
	#define stop_year_len		4	
	
	#define stop_mon_index	(stop_year_index+stop_year_len)
	#define stop_mon_len		2		

	#define stop_day_index		(stop_mon_index+stop_mon_len)
	#define stop_day_len		2	

	#define stop_hour_index	(stop_day_index+stop_day_len)
	#define stop_hour_len		2			

	#define stop_min_index		(stop_hour_index+stop_hour_len)
	#define stop_min_len		2			

	#define stop_sec_index		(stop_min_index+stop_min_len)
	#define stop_sec_len		2	
	ms_stru0(ptm_start, PRSTime);
	ms_stru0(ptm_stop, PRSTime);
	
	#define rstime_startval(pstr,targetbuf,iteam)	 \
		memset(targetbuf,0,sizeof(targetbuf));\
		memcpy(targetbuf,&pstr[start_##iteam##_index],start_##iteam##_len);\
		ptm_start->tm##iteam=atoi(targetbuf);
	
	#define rstime_stopval(pstr,targetbuf,iteam)	 \
		memset(targetbuf,0,sizeof(targetbuf));\
		memcpy(targetbuf,&pstr[stop_##iteam##_index],stop_##iteam##_len);\
		ptm_stop->tm##iteam=atoi(targetbuf);
	
	unsigned char buf[8];
	//ms_debug("DumpDate:%s", pstr);
	
	rstime_startval( pstr, buf,year);
	rstime_startval( pstr, buf,mon);
	rstime_startval( pstr, buf,day);
	rstime_startval( pstr, buf,hour);
	rstime_startval( pstr, buf,min);
	rstime_startval( pstr, buf,sec);
	
	rstime_stopval( pstr, buf,year);
	rstime_stopval( pstr, buf,mon);
	rstime_stopval( pstr, buf,day);
	rstime_stopval( pstr, buf,hour);
	rstime_stopval( pstr, buf,min);
	rstime_stopval( pstr, buf,sec);	
	if(ms_null!=outbuf){
		sprintf(outbuf,"%04d%02d%02d%02d%02d%02d-%04d%02d%02d%02d%02d%02d",ptm_start->tmyear,ptm_start->tmmon,ptm_start->tmday,ptm_start->tmhour,ptm_start->tmmin,ptm_start->tmsec, 
			ptm_stop->tmyear,ptm_stop->tmmon,ptm_stop->tmday,ptm_stop->tmhour,ptm_stop->tmmin,ptm_stop->tmsec);
	}
	ms_verbose("\n\t\t\t\t\t%04d-%02d-%02d %02d:%02d:%02d   \n\t\t\t\t\t%04d-%02d-%02d %02d:%02d:%02d", 
		ptm_start->tmyear,ptm_start->tmmon,ptm_start->tmday,ptm_start->tmhour,ptm_start->tmmin,ptm_start->tmsec, 
		ptm_stop->tmyear,ptm_stop->tmmon,ptm_stop->tmday,ptm_stop->tmhour,ptm_stop->tmmin,ptm_stop->tmsec);

}

ms_void mstime_api_parsetimestr2(ms_string ms_in pstr, PRSTime ms_out prstime)	//2015-06-03 12:30:25
{
	#define year_index		0
	#define year_end		(year_index+year_len)
	#define year_end_len	1	
	
	#define mon_index		(year_end+year_end_len)
	#define mon_end		(mon_index+mon_len)
	#define mon_end_len	1

	#define day_index		(mon_end+mon_end_len)
	#define day_end		(day_index+day_len)
	#define day_end_len	1	

	#define hour_index		(day_end+day_end_len)		
	#define hour_end		(hour_index+hour_len)
	#define hour_end_len		1	

	#define min_index		(hour_end+hour_end_len)
	#define min_end		(min_index+min_len)
	#define min_end_len		1

	#define sec_index		(min_end+min_end_len)	

	#define rstime_val(pstr,targetbuf,iteam)	 \
		memset(targetbuf,0,sizeof(targetbuf));\
		memcpy(targetbuf,&pstr[iteam##_index],iteam##_len);\
		prstime->tm##iteam=atoi(targetbuf);
	
	ms_s08 buf[8];
	//ms_debug("DumpDate:%s", pstr);
	rstime_val( pstr, buf,year);
	rstime_val( pstr, buf,mon);
	rstime_val( pstr, buf,day);
	rstime_val( pstr, buf,hour);
	rstime_val( pstr, buf,min);
	rstime_val( pstr, buf,sec);
}
ms_void mstime_api_parsetimestr3(ms_string ms_in pstr, PRSTime ms_out prstime)	//20150603-12:30:25
{
	#define year_index3		0
	#define year_end3		(year_index3+year_len)
	#define year_end_len3	0	
	
	#define mon_index3	(year_end3+year_end_len3)
	#define mon_end3		(mon_index3+mon_len)
	#define mon_end_len3	0

	#define day_index3		(mon_end3+mon_end_len3)
	#define day_end3		(day_index3+day_len)
	#define day_end_len3	1	

	#define hour_index3	(day_end3+day_end_len3)	
	#define hour_end3		(hour_index3+hour_len)
	#define hour_end_len3		1	

	#define min_index3		(hour_end3+hour_end_len3)
	#define min_end3		(min_index3+min_len)
	#define min_end_len3		1

	#define sec_index3		(min_end3+min_end_len3)	

	#define rstime_val3(pstr,targetbuf,iteam)	 \
		memset(targetbuf,0,sizeof(targetbuf));\
		memcpy(targetbuf,&pstr[iteam##_index3],iteam##_len);\
		prstime->tm##iteam=atoi(targetbuf);
	
	ms_s08 buf[8];
	//ms_debug("DumpDate:%s", pstr);
	rstime_val3( pstr, buf,year);
	rstime_val3( pstr, buf,mon);
	rstime_val3( pstr, buf,day);
	rstime_val3( pstr, buf,hour);
	rstime_val3( pstr, buf,min);
	rstime_val3( pstr, buf,sec);
}

ms_void mstime_api_rstimePrint(PRSTime ms_io prstime)
{
	ms_verbose(mstime_fmt_datetime,
		prstime->tmyear,prstime->tmmon,prstime->tmday,
		prstime->tmhour,prstime->tmmin,prstime->tmsec);
}
ms_u32 mstime_api_rstimeGetSec(RSTime * ms_out prstime)
{
	return (ms_seconds(prstime->tmsec)+ms_minutes(prstime->tmmin)+ms_hours(prstime->tmhour));
}
ms_s64 mstime_api_rstimeMktime(PRSTime ms_in pTm)	//s
{
	struct tm tm1;
	ms_memset(&tm1, 0, ms_bufsize(struct tm));
	tm1.tm_year	=pTm->tmyear-1900;
	tm1.tm_mon	=pTm->tmmon-1;
	tm1.tm_mday	=pTm->tmday;
	tm1.tm_hour	=pTm->tmhour;
	tm1.tm_min	=pTm->tmmin;
	tm1.tm_sec	=pTm->tmsec;
	tm1.tm_isdst = -1;
	return mktime(&tm1);  
}
ms_void mstime_api_rstimeGetCur(RSTime * ms_out prstime)
{
	struct tm p_tm;
	ms_memset(&p_tm, 0, ms_bufsize(struct tm));
	time_t timep=time(ms_null);
#if defined(OS_ANDROID)||defined(OS_LINUX_SOC)		
	localtime_r(&timep,&p_tm);
#else
    	memcpy(&p_tm,localtime(&timep),sizeof(struct tm));
#endif
	prstime->tmyear	=p_tm.tm_year+1900;
	prstime->tmmon	=p_tm.tm_mon+1;
	prstime->tmday	=p_tm.tm_mday;	
	prstime->tmhour	=p_tm.tm_hour;
	prstime->tmmin	=p_tm.tm_min;
	prstime->tmsec	=p_tm.tm_sec;
	prstime->tmwday	=p_tm.tm_wday;
}
ms_u64 mstime_api_us(ms_void)
{
	return mslog_api_us();
}
ms_u64 mstime_api_ms(ms_void)
{
	struct timeval tv;
	ms_memset(&tv, 0, ms_bufsize(struct timeval));
	gettimeofday(&tv, ms_null);
	return (mstime_api_us()/ms_usmseconds(1));
}

ms_u64 mstime_api_sec(ms_void)
{
	return (uint64_t)time((time_t*)ms_null);
}
ms_u32 mstime_api_minute(ms_void)
{
	time_t seconds=mstime_api_sec();
	return (ms_u32)(seconds/(time_t)ms_minutes(1));
}
ms_u32 mstime_api_hour(ms_void)
{
	time_t seconds=mstime_api_sec();
	return (ms_u32)(seconds/(time_t)ms_hours(1));
}
ms_s08 mstime_api_mouth(ms_void)
{
	struct tm p_tm;
	ms_memset(&p_tm, 0, ms_bufsize(struct tm));
	time_t timep=time(ms_null);
#if defined(OS_ANDROID)||defined(OS_LINUX_SOC)		
	localtime_r(&timep,&p_tm);
#else
    memcpy(&p_tm,localtime(&timep),sizeof(struct tm));
#endif
	return p_tm.tm_mon+1;
}
ms_bool mstime_api_setDTime(char *strDateTime)
{
	ms_pamcheckRet(ms_false, strDateTime, "strDateTime");

	struct tm settm;
	ms_memset(&settm, 0, ms_bufsize(struct tm));
	RSTime  settm_tmp;
	mstime_innerapi_parseDateTime(strDateTime, &settm_tmp); //格式20200312143000
	
	settm.tm_sec 		= settm_tmp.tmsec;
	settm.tm_min 		= settm_tmp.tmmin;
	settm.tm_hour 	= settm_tmp.tmhour;
	settm.tm_mday 	= settm_tmp.tmday;
	settm.tm_mon 		= settm_tmp.tmmon - 1;
	settm.tm_year 		= settm_tmp.tmyear - 1900;
	settm.tm_isdst = -1;
	
	struct timeval tv;
	ms_memset(&tv, 0, ms_bufsize(struct timeval));
	tv.tv_sec		=mktime(&settm);
	tv.tv_usec = 0;
	if(settimeofday (&tv, (struct timezone *) 0) < 0){
		ms_errRet(ms_false, "set system datatime to  %s   error",strDateTime);
	}
//硬件时钟同步
	ms_system("hwclock --systohc");
	return ms_true;
}
/*获取年月日时分秒，格式为frm*/
ms_string mstime_api_curDTime(ms_string ms_in frm,ms_string ms_out strCurDTime)
{
	return mslog_api_curDTime( frm, strCurDTime);
}

/*获取年月日，格式为frm*/
ms_string mstime_api_curDate(ms_string ms_in frm,ms_string ms_out str_curDate)
{
	struct tm p_tm;
	ms_memset(&p_tm, 0, ms_bufsize(struct tm));
	time_t timep=time(ms_null);
#if defined(OS_ANDROID)||defined(OS_LINUX_SOC)		
	localtime_r(&timep,&p_tm);
#else
    memcpy(&p_tm,localtime(&timep),sizeof(struct tm));
#endif
	ms_sprintfs((ms_string)str_curDate, frm,(p_tm.tm_year+1900),(p_tm.tm_mon+1),(p_tm.tm_mday));
	return str_curDate;
}
/*获取年月，格式为frm*/
ms_string mstime_api_curYM(ms_string ms_in frm,ms_string ms_out str_curYM)
{
	struct tm p_tm;
	ms_memset(&p_tm, 0, ms_bufsize(struct tm));
	time_t timep=time(ms_null);
#if defined(OS_ANDROID)||defined(OS_LINUX_SOC)		
	localtime_r(&timep,&p_tm);
#else
    memcpy(&p_tm,localtime(&timep),sizeof(struct tm));
#endif
	ms_sprintfs((ms_string)str_curYM, frm,(p_tm.tm_year+1900),(p_tm.tm_mon+1));
	return str_curYM;
}

/*获取时分秒，格式为frm*/
ms_string mstime_api_curTime(ms_string ms_in frm,ms_string ms_out str_curtime)
{
	struct tm p_tm;
	ms_memset(&p_tm, 0, ms_bufsize(struct tm));
	time_t timep=time(ms_null);
#if defined(OS_ANDROID)||defined(OS_LINUX_SOC)		
	localtime_r(&timep,&p_tm);
#else
    	memcpy(&p_tm,localtime(&timep),sizeof(struct tm));
#endif
	ms_sprintfs((ms_string)str_curtime, frm,(p_tm.tm_hour),(p_tm.tm_min),(p_tm.tm_sec));
	return str_curtime;
}

/*将输入的us时间戳，转化为frm格式的年月日时分秒*/
ms_void mstime_api_us2DTime(ms_u64 * ms_in timestampUs,ms_string ms_in frm,char * ms_out strDTime)
{
	struct tm p_tm;
	ms_memset(&p_tm, 0, ms_bufsize(struct tm));
	memset(strDTime, 0, sizeof(strDTime) );
	
	ms_u64 timepsec=(*timestampUs)/ms_usseconds(1);
#if defined(OS_ANDROID)||defined(OS_LINUX_SOC)		
	localtime_r((const time_t*)&timepsec,&p_tm);
#else
	memcpy(&p_tm,localtime(&timepsec),sizeof(struct tm));
#endif
	sprintf((char*)strDTime, frm,(p_tm.tm_year+1900),(p_tm.tm_mon+1),(p_tm.tm_mday),(p_tm.tm_hour),(p_tm.tm_min),(p_tm.tm_sec));
}
/*将输入的sec时间戳，转化为frm格式的年月日时分秒*/
ms_void mstime_api_sec2DTime(ms_u64 * timestampSec,ms_string ms_in frm,char * strDTime)
{
	struct tm p_tm;
	ms_memset(&p_tm, 0, ms_bufsize(struct tm));
	memset(strDTime, 0, sizeof(strDTime) );
#if defined(OS_ANDROID)||defined(OS_LINUX_SOC)		
	localtime_r((const time_t*)timestampSec,&p_tm);
#else
	memcpy(&p_tm,localtime(timestampSec),sizeof(struct tm));
#endif
	sprintf((char*)strDTime, frm,(p_tm.tm_year+1900),(p_tm.tm_mon+1),(p_tm.tm_mday),(p_tm.tm_hour),(p_tm.tm_min),(p_tm.tm_sec));
}
ms_void mstime_api_us2str(ms_u64 * timestampUs,ms_string ms_in frm,char * strDTime)
{
	struct tm p_tm;
	ms_memset(&p_tm, 0, ms_bufsize(struct tm));
	memset(strDTime, 0, sizeof(strDTime) );
	
	ms_u64 timepsec=(*timestampUs)/1000000;
#if defined(OS_ANDROID)||defined(OS_LINUX_SOC)		
	localtime_r((const time_t*)&timepsec,&p_tm);
#else
	memcpy(&p_tm,localtime(&timepsec),sizeof(struct tm));
#endif
	sprintf((char*)strDTime, frm,(p_tm.tm_hour),(p_tm.tm_min),(p_tm.tm_sec));
}
ms_string mstime_api_us2str2(ms_s64 ms_in timeus,ms_string outbuf)
{
	return mslog_api_us2time(timeus,outbuf,".%d","%d(s).%d.%d");;
}
ms_string mstime_api_ms2str2(ms_u64 ms_in timeMs,ms_string outbuf)
{
	ms_memset(outbuf, 0, sizeof(outbuf));
	if(-1==timeMs){
		ms_strcat(outbuf, "unvalid");
	}else{
		ms_u32 oneday	=ms_msseconds(60*60*24);
		ms_u32 onehour	=ms_msseconds(60*60);
		ms_u32 oneminute	=ms_msseconds(60);
		ms_u32 onesecond	=ms_msseconds(1);
		ms_u32 days   =(timeMs/oneday);
		ms_u32 hours =(timeMs%oneday)/onehour;
		ms_u32 minutes=((timeMs%oneday)%onehour)/oneminute;
		ms_u32 secend=(((timeMs%oneday)%onehour)%oneminute)/onesecond;
		ms_u32 msecend=(((timeMs%oneday)%onehour)%oneminute)%onesecond;
		ms_u08 tbuf[256]={0};
		if(days>0){
			ms_strcats(tbuf, outbuf, "%d_", days);
		}
		ms_strcats(tbuf, outbuf, "%d:%d:%d.%d", hours,minutes,secend,msecend);
	}
	return outbuf;
}

ms_void mstime_api_sec2str(ms_u64 * timestampSec,ms_string ms_in frm,char * strDTime)
{
	struct tm p_tm;
	ms_memset(&p_tm, 0, ms_bufsize(struct tm));
	memset(strDTime, 0, sizeof(strDTime) );
#if defined(OS_ANDROID)||defined(OS_LINUX_SOC)		
	localtime_r((const time_t*)timestampSec,&p_tm);
#else
	memcpy(&p_tm,localtime(timestampSec),sizeof(struct tm));
#endif
	sprintf((char*)strDTime, frm,(p_tm.tm_hour),(p_tm.tm_min),(p_tm.tm_sec));
}
ms_string mstime_api_sec2str2(ms_u32 ms_in timesec,ms_string outbuf)
{
	return mslog_api_sec2Time(timesec, outbuf);
}

ms_string mstime_api_rtstime2DTime(ms_string ms_in fmt,RSTime ms_in rstime,ms_string outstr)
{
	ms_pamcheckRet(ms_null, outstr, "outstr");
	sprintf((ms_string)outstr, fmt,rstime.tmyear,rstime.tmmon,rstime.tmday,rstime.tmhour,rstime.tmmin,rstime.tmsec);
	return outstr;
}

/*CLOCK_REALTIME：系统相对时间，从UTC 1970-1-1 0:0:0开始计时，更改系统时间会影响该值
CLOCK_MONOTONIC：系统绝对时间/单调时间，为系统重启到现在的时间，更改系统时间对它没有影响
CLOCK_PROCESS_CPUTIME_ID：本进程到当前代码系统花费的时间；
CLOCK_THREAD_CPUTIME_ID：本线程到当前代码系统花费的时间；*/
ms_u64 mstime_api_upTimeUs(ms_void)
{
	struct timespec ts;
	(void)clock_gettime(CLOCK_MONOTONIC,&ts);
	ms_u64 upTimeUs=(ts.tv_sec*1000*1000)+(ts.tv_nsec/1000);
	return upTimeUs;
}
ms_u64 mstime_api_upTimeMs(ms_void)
{
	ms_u64 upTimeUs=mstime_api_upTimeUs();
	return (upTimeUs/1000);
}
ms_u64 mstime_api_upTimeSec(ms_void)
{
	ms_u64 upTimeMs=mstime_api_upTimeMs();
	return (upTimeMs/1000);
}
ms_bool mstime_api_isNewLoop(ms_string ms_in pstr,ms_s32 timeout,ms_bool *flag_cur,ms_bool *flag_pre)	//s
{
#if defined(OS_LINUX_SOC)
	RSTime rstime;
	memset(&rstime,0,sizeof(RSTime));
	mstime_api_parsetimestr3(pstr, &rstime);
	RSTime rs_cur;
	memset(&rs_cur,0,sizeof(RSTime));
	mstime_api_rstimeGetCur(&rs_cur);
	ms_u32 cur_sec=rs_cur.tmhour*3600+rs_cur.tmmin*60+rs_cur.tmsec;
	ms_u32 target_sec=rstime.tmhour*3600+rstime.tmmin*60+rstime.tmsec;
	ms_u32 max_sec=24*3600;
	if((target_sec+timeout)>max_sec){
		cur_sec+=max_sec;
	}
	if( ms_range(cur_sec, target_sec, (target_sec+timeout))){
		(*flag_cur)=ms_true;
	}else{
		(*flag_cur)=ms_false;
	}

#if 0
	ms_debug("%s",pstr);
	ms_debug("rstime:%02d:%02d:%02d",rstime.tmhour ,rstime.tmmin ,rstime.tmsec);
	ms_debug("rs_cur:%02d:%02d:%02d",rs_cur.tmhour ,rs_cur.tmmin ,rs_cur.tmsec);
	ms_debug("cur_sec,target_sec,(*flag_cur),(*flag_pre):  %d %d   %d  %d",cur_sec,target_sec,(*flag_cur),(*flag_pre));
#endif
	if((*flag_cur)!=(*flag_pre)){
		(*flag_pre)=(*flag_cur);
		return (*flag_cur) ;
	}
#endif
	return ms_false;
} 

ms_bool mstime_api_isNewDay(time_t * ms_io timep_pre)	//s
{
	struct tm p_tm;
	ms_memset(&p_tm, 0, ms_bufsize(struct tm));
	time_t timep=time(ms_null);
#if defined(OS_ANDROID)||defined(OS_LINUX_SOC)		
	localtime_r(&timep,&p_tm);
#else
	ms_memcpy(&p_tm,localtime(&timep),sizeof(struct tm));
#endif
//	tm_yday
	ms_bool ret=ms_false;
	if(0==(*timep_pre)){
		if(  (0==p_tm.tm_hour)&&(0==p_tm.tm_min)){
			(*timep_pre)=timep;
			ret=ms_true;
		}
	}else{
		if( !((timep-(*timep_pre))<(24*60*60))){
			(*timep_pre)=timep;
			ret=ms_true;
		}
	}
	return ret;
} 

ms_bool mstime_api_isWholeClock(time_t *ms_io timep_pre)
{
	struct tm p_tm;
	time_t timep=time(ms_null);
	ms_memset(&p_tm, 0, ms_bufsize(struct tm));
#if defined(OS_ANDROID)||defined(OS_LINUX_SOC)		
	localtime_r(&timep,&p_tm);
#else
	memcpy(&p_tm,localtime(&timep),sizeof(struct tm));
#endif
	ms_bool ret=ms_false;
	if(0==(*timep_pre)){
		//BUG：在获取时间基点前，无法应对系统时钟的跳动
		if( (30==p_tm.tm_min)||(0==p_tm.tm_min)){//获取基准点
			(*timep_pre)=timep;
			ret=ms_true;
		}
	}else{
		if( ms_noLess(ms_abs(timep-(*timep_pre)), (30*60))){//获取基准点后，不能再已分钟点数进行判断（如10:30:11获取基准点，10:30:50秒再次进行判断）
			(*timep_pre)=timep;
			ret=ms_true;
		}
	}
	return ret;
}

ms_bool mstime_api_isVaildDate( RSTime ms_in rstime_now,ms_string ms_in start_date,ms_string ms_in end_date)
{	
	RSTime rstime_startdate;
	RSTime rstime_enddate;
	ms_memset(&rstime_startdate, 0, ms_bufsize(RSTime));
	ms_memset(&rstime_enddate, 0, ms_bufsize(RSTime));
	mstime_innerapi_parseDate(start_date, &rstime_startdate);
	mstime_innerapi_parseDate(end_date, &rstime_enddate);	
#if DEBUG_FILTERS_FRESH	
ms_debug("rstime_now: %d-%d-%d",rstime_now.tmyear,rstime_now.tmmon,rstime_now.tmday);
ms_debug("rstime_startdate: %d-%d-%d",rstime_startdate.tmyear,rstime_startdate.tmmon,rstime_startdate.tmday);
ms_debug("rstime_enddate: %d-%d-%d",rstime_enddate.tmyear,rstime_enddate.tmmon,rstime_enddate.tmday);
#endif
	if(rstime_now.tmyear<rstime_startdate.tmyear ){
		return ms_false;
	}else if(  rstime_now.tmyear==rstime_startdate.tmyear
		&&rstime_now.tmmon<rstime_startdate.tmmon){
		return ms_false;
	}else if(  rstime_now.tmyear==rstime_startdate.tmyear
		&&rstime_now.tmmon==rstime_startdate.tmmon
		&&rstime_now.tmday<rstime_startdate.tmday){
		return ms_false;
	}


	if(rstime_now.tmyear>rstime_enddate.tmyear ){
		return ms_false;
	}else if(  rstime_now.tmyear==rstime_enddate.tmyear
		&&rstime_now.tmmon>rstime_enddate.tmmon){
		return ms_false;
	}else if(  rstime_now.tmyear==rstime_enddate.tmyear
		&&rstime_now.tmmon==rstime_enddate.tmmon
		&&rstime_now.tmday>rstime_enddate.tmday){
		return ms_false;
	}

	return ms_true;
}

ms_bool mstime_api_isVaildTime( RSTime ms_in rstime_now,ms_string ms_in start_time,ms_string ms_in end_time)
{
	RSTime rstime_starttime;
	RSTime rstime_endtime;
	ms_memset(&rstime_starttime, 0, ms_bufsize(RSTime));
	ms_memset(&rstime_endtime, 0, ms_bufsize(RSTime));
	mstime_innerapi_parseTime(start_time, &rstime_starttime);
	mstime_innerapi_parseTime(end_time, &rstime_endtime);	
#if DEBUG_FILTERS_FRESH	
ms_debug("rstime_now: %d:%d:%d",rstime_now.tmhour,rstime_now.tmmin,rstime_now.tmsec);
ms_debug("rstime_starttime: %d:%d:%d",rstime_starttime.tmhour,rstime_starttime.tmmin,rstime_starttime.tmsec);
ms_debug("rstime_endtime: %d:%d:%d",rstime_endtime.tmhour,rstime_endtime.tmmin,rstime_endtime.tmsec);
#endif
	ms_s64 sec_nowtime=rstime_now.tmhour*3600+rstime_now.tmmin*60+rstime_now.tmsec;
	ms_s64 sec_starttime=rstime_starttime.tmhour*3600+rstime_starttime.tmmin*60+rstime_starttime.tmsec;
	ms_s64 sec_endtime=rstime_endtime.tmhour*3600+rstime_endtime.tmmin*60+rstime_endtime.tmsec;
	
	if( !(sec_nowtime<sec_starttime )
		&&!(sec_nowtime>sec_endtime)){
		return ms_true;
	}else{
		return ms_false;
	}
}
ms_bool mstime_api_isVaildWDay(RSTime ms_in rstime_now, ms_s32 ms_in vaildwday)
{
#if DEBUG_FILTERS_FRESH	
	ms_debug("vaildwday,tmwday,is_vaildwday: %03d-(%#x)  ,%d   ,%d",vaildwday,vaildwday,rstime_now.tmwday,(vaildwday & (1<<rstime_now.tmwday)));
#endif
	if(vaildwday & (1<<rstime_now.tmwday)){
		return ms_true;
	}else{
		return ms_false;
	}
}
ms_bool mstime_api_isPass(PRSTime ms_in pTm)	//s
{
	return (mstime_api_sec()<mstime_api_rstimeMktime(pTm)) ? ms_false : ms_true;
} 
ms_bool mstime_api_counterAsyncUs(ms_u64 * ms_in Etimep,ms_u64 ms_in time_us)	//us
{
	ms_u64 cur_timep=mstime_api_us();
	if(cur_timep<(*Etimep)){
	//系统时间被更改到过去，立马同步
		(*Etimep)=cur_timep;
		return ms_true;
	}else{
		if(cur_timep>((*Etimep)+time_us)){
			(*Etimep)=cur_timep;
			return ms_true;
		}else{
			return ms_false;
		}
	}
} 
ms_bool mstime_api_counterAsyncSec(time_t * ms_in Etimep,ms_u32 ms_in sec)	//s
{
	return mslog_api_timerAsyncSec(Etimep, sec);
} 
ms_bool mstime_api_moreThan(PRSTime ms_in pTm1,PRSTime ms_in pTm2)	//s
{
	return (mstime_api_rstimeMktime(pTm1)>mstime_api_rstimeMktime(pTm2)) ? ms_true : ms_false;
} 
ms_bool mstime_api_timeout(PRSTime ms_in ptm_start,PRSTime ms_in ptm_stop,ms_u64 timeou_sec)	//s
{
	return ( (mstime_api_rstimeMktime(ptm_stop)-mstime_api_rstimeMktime(ptm_start)) >timeou_sec) ? ms_true : ms_false;
}
ms_bool mstime_api_timeout2(ms_u64 time1,ms_u64 time2,ms_u64 timeout)	
{
	if((time1>(time2+timeout))||(time2>(time1+timeout))){
		return ms_true;
	}else{
		return ms_false;
	}
}
ms_bool mstime_api_timeout3(PRSTime ms_in ptm_start,PRSTime ms_in ptm_stop,ms_u64 timeou_sec)	//s
{
	ms_bool ret=ms_false;
	if(ms_true==mstime_api_moreThan(ptm_stop,ptm_start)){
		ret=( (mstime_api_rstimeMktime(ptm_stop)-mstime_api_rstimeMktime(ptm_start)) >timeou_sec) ? ms_true : ms_false;
	}else{
		ret=( (mstime_api_rstimeMktime(ptm_start)-mstime_api_rstimeMktime(ptm_stop)) >timeou_sec) ? ms_true : ms_false;
	}
	return ret;
}
ms_bool mstime_api_disorder(ms_u64 *cur_time,ms_u64 *pass_time,ms_u64 timeout_us)	
{
	(*pass_time)=(*cur_time);
	(*cur_time)=mstime_api_us();
	//ms_debug("cur_time,pass_time:%s,%s",ms_num2str((*cur_time)),ms_num2str((*pass_time)));
	if(ms_lessThan((*cur_time), (*pass_time))){//时间成为过去
		return ms_true;
	}else if(ms_moreThan(((*cur_time)-(*pass_time)), timeout_us)){//时间突然往前跳跃timeout_us
		return ms_true;
	}
	return ms_false;
}
ms_void mstime_api_cast(ms_u64 *preTime_us,ms_string light,ms_cstring ms_in func,ms_u32 ms_in line)
{
	ms_u64 curTime=mstime_api_us();
	if(light){
		if(0==(*preTime_us)){
			ms_info("%s开始计算消耗----func:%s,line:%d",light,func,line);
		}else{
			ms_info("%s消耗%s微妙----func:%s,line:%d",light,ms_num2str((curTime-(*preTime_us))),func,line);
		}
	}else{
		if(0==(*preTime_us)){
			ms_info("开始计算消耗----func:%s,line:%d",func,line);
		}else{
			ms_info("消耗%s微妙----func:%s,line:%d",ms_num2str((curTime-(*preTime_us))),func,line);
		}
	}
	(*preTime_us)=curTime;
}
#undef MSTIME_C
